//
//  YDYStatusViewModel.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/22.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYStatusViewModel.h"
#import <SDWebImageManager.h>
@implementation YDYStatusViewModel
-(void)setStatus:(YDYStatus *)status{
    _status = status;
    //1.处理创建时间
    [self handleCreated_At];
    //2.ID
    [self handleID];
    //3.文字内容
    [self handleContentText];
    //4.处理来源
    [self handleSource];
    //5.处理用户
    [self handleUser];
    //6.处理图片地址数组
    [self handlePic_urls];
    
    //其他操作
//    [self others];
}



//-(void)others{
//    //1.缓存图片
//    [self cacheImages];
//}
-(void)handlePic_urls{
    if (self.status.pic_urls.count == 0)return;
    NSMutableArray *urlArray = [NSMutableArray array];
    for (NSDictionary *dict in self.status.pic_urls) {
        NSString *imgURLString = dict[@"thumbnail_pic"];
        NSURL *imgurl = [NSURL URLWithString:imgURLString];
        [urlArray addObject:imgurl];
    }
    self.pic_urls = urlArray;
    
}
-(void)handleUser{
    self.user = self.status.user;
}
-(void)handleSource{
    //@"<a href=\"http://app.weibo.com/t/feed/3jskmg\" rel=\"nofollow\">iPhone 6s</a>"	0x0000618000056a40
    NSString *substr = @"神奇的国度";
    if (self.status.source.length != 0) {
        NSInteger startindex = [self.status.source rangeOfString:@">"].location +1;
        NSInteger length = [self.status.source rangeOfString:@"</"].location -startindex;
        substr = [self.status.source substringWithRange:NSMakeRange(startindex, length)];
    }
    self.source = substr;
}
-(void)handleContentText{
    self.text = self.status.text;
}
-(void)handleID{
    self.idStr = self.status.idStr;
}



-(void)handleCreated_At{
    
    NSString *timeText = @"??";
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    //@"Mon Aug 22 13:36:24 +0800 2016"	0x00006080000595f0
    formatter.dateFormat = @"EE MM dd HH:mm:ss Z yyyy";
    
    NSDate *creatDate = [formatter dateFromString:self.status.created_at];
    NSInteger interval =(int) [[[NSDate alloc]init] timeIntervalSinceDate:creatDate];
    
    /**
     1.应该扩展NSCalendar 类
     NSCalendar *cal = [NSCalendar currentCalendar];
     NSDateComponents *com = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:creatDate];
     YDYLog(@"%ld,%ld,%ld",com.year,com.minute,com.second);
     
     BOOL a = [cal isDateInToday:creatDate];*/
    
    //下面的代码可以用
    if (interval < 60) {
        timeText = @"刚刚";
    }else if (interval < 60 * 60){
        timeText = [NSString stringWithFormat:@"%ld分钟前",interval / 60];
    }else if (interval < 60 * 60 * 24){
        timeText = [NSString stringWithFormat:@"%ld小时前",interval / (60 * 60)];
    }else if (interval < 60 * 60 * 24 * 30){
        timeText = [NSString stringWithFormat:@"%ld天前",interval / (60 * 60 * 24)];
    }else{
        timeText = @"很久之前";
    }
    
    _created_at = timeText;
}

#pragma mark -
#pragma mark -初始化方法
-(instancetype)initWithYDYStatus:(YDYStatus *)status{
    self.status  = status;
    return self;
}
+(instancetype)YDYStatusViewModelWithStatus:(YDYStatus *)status{
    return [[self alloc]initWithYDYStatus:status];
}
#pragma mark -
#pragma mark -设置其他，用到的方法
-(void)cacheImages:(void(^)())cacheFinished{
    if (self.pic_urls.count == 0) return;
    
    dispatch_group_t group =  dispatch_group_create();
    for (NSURL *url in self.pic_urls) {
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager]downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            dispatch_group_leave(group);
        }];
        
    }
    //一条微博图片下载完成后会调用
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        cacheFinished();
        YDYLog(@"一条微博所有图片缓存完成");
    });
}
@end
